package com.danieldura.highrise.data
{
	import com.adobe.utils.DateUtil;
	
	public class Person implements HighriseData
	{
		public static const VISIBLE_EVERYONE:String = "Everyone";
		public static const VISIBLE_OWNER:String = "Owner";
		public static const VISIBLE_NAMEDGROUP:String = "NamedGroup";		
		
		public var id:int;
		public var firstName:String;
		public var lastName:String;
		public var title:String;
		public var background:String;
		public var companyId:int;
		public var createdAt:Date;
		public var updatedAt:Date;
		public var visibleTo:String;
		public var ownerId:int;
		public var groupId:int;
		public var authorId:int;
		public var contactData:ContactData;
		
		public function get avatarUrl():String
		{
			var addZeros:int = 8 - id.toString().length;
			var idString:String = id.toString();
			
			for( var i:int = 0; i < addZeros; i++ )
				idString = "0" + idString;
				
			return "https://asset0.highrisehq.com/avatars/person/" + idString.substr( 0, 4 ) + "/" + idString.substr( 4, 4 ) + "-large.png";
		}		
		
		public function parse( data:XML ):void
		{
			id 			= data.id;
			firstName 	= data["first-name"];
			lastName 	= data["last-name"];
			title		= data.title;
			background 	= data.background;
			companyId	= data["company-id"];
			createdAt	= DateUtil.parseW3CDTF( data["created-at"] );
			updatedAt	= DateUtil.parseW3CDTF( data["updated-at"] );
			visibleTo 	= data["visible-to"];
			ownerId		= data["owner-id"];
			groupId		= data["group-id"];
			authorId	= data["author-id"];
			
			contactData = new ContactData();
			contactData.parse( data["contact-data"][0] );			
		}
		
		public function toXML():XML
		{
			var person:XML = new XML( <person></person> );
			person.id = id;
			person["first-name"] = firstName;
			person["last-name"] = lastName;
			person.title = title;
			person.background = background;
			person["company-id"] = companyId;
			person["visible-to"] = visibleTo;
			person["owner-id"] = ownerId;
			person["group-id"] = groupId;
			person["author-id"] = authorId;
			
			person.appendChild( contactData.toXML() );

			return person;
		}
		
		public function toXMLString():String
		{
			return toXML().toXMLString();
		}
	}
}

/*
<person>
  <id type="integer">1</id>
  <first-name>John</first-name>
  <last-name>Doe</last-name>
  <title>Stand-in</title>
  <background>A popular guy for random data</background>
  <company-id type="integer">2</company-id>
  <created-at type="datetime">2007-02-27T03:11:52Z</created-at>
  <updated-at type="datetime">2007-03-10T15:11:52Z</updated-at>
  <visible-to>#{Everyone || Owner || NamedGroup}</visible-to>
  <owner-id type="integer">#{ user_id -- requires visble-to is "Owner"}</owner-id>
  <group-id type="integer">#{ group_id -- requires visble-to is "NamedGroup"}</group-id>
  <author-id type="integer">3</author-id>
  <contact-data>
    ...
  </contact-data>
</person>
*/